﻿
namespace GroupChallenge.Web
{
    // Partial class addition to the server's generated partial class for Player Image.
    // This class adds functionality needed to identify the player id from the file name with the id in it.
    public sealed partial class PlayerImage
    {
        private const string _FilePrefixLowered = "player_";

        private int _PlayerId = 0;
        public int PlayerId
        {
            get
            {
                return _PlayerId;
            }
            set
            {
                if ((_PlayerId != value))
                {
                    _PlayerId = value;

                    RaiseDataMemberChanged("PlayerId");
                }
            }
        }

        // Get the player id from the specified file name with the file name in it.
        public int GetPlayerIdFromFile(string fileName)
        {
            int ret = 0;

            if (!string.IsNullOrEmpty(fileName))
            {
                fileName = fileName.ToLowerInvariant();

                if (fileName.StartsWith(_FilePrefixLowered))
                {
                    // Remove right side, regardless of the image extension type.
                    int pos = fileName.LastIndexOf(".");
                    if (pos > 0)                // At least once character.
                    {
                        int start = _FilePrefixLowered.Length;
                        int length = pos - _FilePrefixLowered.Length;

                        if (length > 0)
                        {
                            string numberText = fileName.Substring(_FilePrefixLowered.Length, length);

                            int.TryParse(numberText, out ret);
                        }
                    }
                }
            }

            return ret;
        }
    }
}
